package com.ctshk.rpc.system.mapper;

import java.util.List;

import com.ctshk.rpc.system.base.CustomBaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.rpc.system.dto.QuerySupplierAndAgentDTO;
import com.ctshk.rpc.system.dto.SearchSupplierDTO;
import com.ctshk.rpc.system.dto.SysSupplierQueryListDTO;
import com.ctshk.rpc.system.entity.SysSupplier;
import com.ctshk.rpc.system.req.SearchSupplierReq;
import com.ctshk.rpc.system.req.SysSupplierQueryListReq;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 供应商 Mapper 接口
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-05
 */
public interface SysSupplierMapper extends CustomBaseMapper<SysSupplier> {


    @Select("<script>"+"select c.id id, c.cn_name groupCnName,c.en_name groupEnName,c.platform_sys_type platformSysType,c.status status,c.check_status checkStatus,c.modified_id modifiedId,c.gmt_modified gmtModified,su.cn_surname as cnSurname,su.cn_name as cnName from t_sys_supplier c  LEFT JOIN t_sys_user su ON c.modified_id = su.id WHERE c.is_deleted = 0\n" +
            "<if test=\"req.groupName != null and req.groupName != ''\"> and (c.cn_name LIKE CONCAT('%',#{req.groupName},'%') or c.en_name LIKE CONCAT('%',#{req.groupName},'%'))</if>\n" +
            "<if test=\"req.status != null\"> AND c.status = #{req.status}</if>\n" +
            "<if test=\"req.checkStatus != null\"> AND c.check_status = #{req.checkStatus}</if>\n" +
            "<if test=\"req.platformSysType != null\"> AND c.platform_sys_type = #{req.platformSysType}</if>\n" +
            "<if test=\"req.isNotAdmin == 1\"> and c.create_id in <foreach collection=\"req.userIds\" item=\"item\" open=\"(\" close=\")\" separator=\",\">#{item}</foreach></if>\n" +
            "ORDER BY c.gmt_create DESC"+"</script>")
    IPage<SysSupplierQueryListDTO> queryList(SysSupplierQueryListReq req, IPage<SysSupplierQueryListDTO> page);

	/**通过名称查询供应商和代理商
	 * @param name
	 * @return
	 */
    @Select("SELECT id,cn_name as cnName,1 as type  FROM t_sys_supplier WHERE is_deleted=#{isDelete} AND cn_name=#{name} and check_status=#{checkStatus}  and status=#{status} UNION SELECT id,cn_name as cnName,2 as type FROM t_sys_agent  WHERE is_deleted=#{isDelete} AND cn_name=#{name}  and check_status=#{checkStatus}  and status=#{status}")
	List<QuerySupplierAndAgentDTO> querySupplierAndAgent(String name,Integer checkStatus,Integer isDelete,Integer status);

    @Select("SELECT * FROM t_sys_supplier as ss WHERE  check_status=#{checkStatus}  and status=#{status} and ss.cn_name LIKE CONCAT('%',#{name},'%') AND ss.is_deleted=#{isDelete} "+
    		"AND EXISTS (SELECT 1 FROM t_sys_supplier_cost_item as ssci WHERE ssci.supplier_id=ss.id AND ssci.cost_item_id=#{costItemId})")
	List<SysSupplier> querySupplierByNameAndCostItem(String name, String costItemId,Integer checkStatus,Integer isDelete,Integer status);

    @Select("SELECT id,cn_name as groupCnName FROM t_sys_supplier WHERE is_deleted=#{isDelete} AND cn_name LIKE CONCAT('%',#{req.name},'%') and check_status=#{req.checkStatus}  and status=#{req.status}")
	IPage<SearchSupplierDTO> searchByName(SearchSupplierReq req, IPage<SearchSupplierDTO> page,Integer isDelete);

	@Update("UPDATE t_sys_supplier SET is_deleted = 1 WHERE id = #{id}")
	int deleteById(@Param("id") Long Id);
}
